package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.CustomerTraceHistory;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {

    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;
    @Override
    public void delete(Long id) {
        customerTraceHistoryMapper.deleteByPrimaryKey(id);
    }

    //因为跟进客户其实就是加一条与客户交流的记录
    @Override
    public void save(CustomerTraceHistory customerTraceHistory) {
        //获取当前登录用户
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        //设置录入人为当前登录用户
        customerTraceHistory.setInputUser(employee);
        //将当前系统时间设置为录入时间
        customerTraceHistory.setInputTime(new Date());
        customerTraceHistoryMapper.insert(customerTraceHistory);
    }

    @Override
    public CustomerTraceHistory getOne(Long id) {
        return customerTraceHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTraceHistory> getAll() {
        return customerTraceHistoryMapper.selectAll();
    }

    @Override
    public void update(CustomerTraceHistory customerTraceHistory) {
        customerTraceHistoryMapper.updateByPrimaryKey(customerTraceHistory);
    }


    @Override
    public PageInfo<CustomerTraceHistory> selctForPageResult(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),"input_time desc");
        List<CustomerTraceHistory> customerTraceHistorys = customerTraceHistoryMapper.selectForResult(qo);
        return new PageInfo(customerTraceHistorys);
    }

}
